package com.ruoyi.client.service.impl;

import com.ruoyi.client.domain.UserShippingAddress;
import com.ruoyi.client.domain.vo.request.ClientAddressAddRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressDeleteRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressSetDefaultRequestVo;
import com.ruoyi.client.domain.vo.request.ClientAddressUpdateRequestVo;
import com.ruoyi.client.domain.vo.response.ClientAddressResponseVo;
import com.ruoyi.client.mapper.UserShippingAddressMapper;
import com.ruoyi.client.service.IUserShippingAddressService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户收货地址Service实现
 * 
 * @author ruoyi
 * @date 2024-02-24
 */
@Service
public class UserShippingAddressServiceImpl implements IUserShippingAddressService {
    @Autowired
    private UserShippingAddressMapper addressMapper;

    /**
     * 查询用户收货地址列表
     * 
     * @param userId 用户ID
     * @return 收货地址集合
     */
    @Override
    public List<ClientAddressResponseVo> getUserAddressList(String userId) {
        List<UserShippingAddress> addressList = addressMapper.selectUserAddressList(userId);
        List<ClientAddressResponseVo> voList = new ArrayList<>();
        
        for (UserShippingAddress address : addressList) {
            ClientAddressResponseVo vo = new ClientAddressResponseVo();
            BeanUtils.copyProperties(address, vo);
            voList.add(vo);
        }
        
        return voList;
    }

    /**
     * 查询用户收货地址
     * 
     * @param id 收货地址ID
     * @return 收货地址
     */
    @Override
    public ClientAddressResponseVo getUserAddressById(Long id) {
        UserShippingAddress address = addressMapper.selectUserAddressById(id);
        if (address == null) {
            return null;
        }
        
        ClientAddressResponseVo vo = new ClientAddressResponseVo();
        BeanUtils.copyProperties(address, vo);
        return vo;
    }

    /**
     * 查询用户默认收货地址
     * 
     * @param userId 用户ID
     * @return 默认收货地址
     */
    @Override
    public ClientAddressResponseVo getDefaultAddress(String userId) {
        UserShippingAddress address = addressMapper.selectDefaultAddress(userId);
        if (address == null) {
            return null;
        }
        
        ClientAddressResponseVo vo = new ClientAddressResponseVo();
        BeanUtils.copyProperties(address, vo);
        return vo;
    }

    /**
     * 新增收货地址
     * 
     * @param requestVo 收货地址信息
     * @return 结果
     */
    @Override
    @Transactional
    public int addAddress(ClientAddressAddRequestVo requestVo) {
        // 检查地址数量是否达到上限
        int count = addressMapper.countUserAddress(requestVo.getUserId());
        if (count >= 10) {
            return -1; // 地址数量达到上限
        }
        
        UserShippingAddress address = new UserShippingAddress();
        BeanUtils.copyProperties(requestVo, address);
        
        // 如果是默认地址，先重置其他默认地址
        if (address.getIsDefault() != null && address.getIsDefault() == true) {
            addressMapper.resetDefaultAddress(address.getUserId());
        }
        // 如果是第一个地址，自动设为默认地址
        else if (count == 0) {
            address.setIsDefault(true);
        }
        
        return addressMapper.insertAddress(address);
    }

    /**
     * 修改收货地址
     * 
     * @param requestVo 收货地址信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateAddress(ClientAddressUpdateRequestVo requestVo) {
        UserShippingAddress address = new UserShippingAddress();
        BeanUtils.copyProperties(requestVo, address);
        
        // 如果设置为默认地址，先重置其他默认地址
        if (address.getIsDefault() != null && address.getIsDefault() == true) {
            addressMapper.resetDefaultAddress(address.getUserId());
        }
        
        return addressMapper.updateAddress(address);
    }

    /**
     * 删除收货地址
     * 
     * @param requestVo 删除请求信息
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAddress(ClientAddressDeleteRequestVo requestVo) {
        // 查询要删除的地址
        UserShippingAddress address = addressMapper.selectUserAddressById(requestVo.getId());
        if (address == null || !address.getUserId().equals(requestVo.getUserId())) {
            return 0;
        }
        
        // 删除地址
        int rows = addressMapper.deleteAddress(requestVo.getId(), requestVo.getUserId());
        
        // 如果删除的是默认地址，则将最新的地址设为默认地址
        if (rows > 0 && address.getIsDefault() == true) {
            List<UserShippingAddress> addressList = addressMapper.selectUserAddressList(requestVo.getUserId());
            if (!addressList.isEmpty()) {
                addressMapper.setDefaultAddress(addressList.get(0).getId(), requestVo.getUserId());
            }
        }
        
        return rows;
    }

    /**
     * 设置默认地址
     * 
     * @param requestVo 设置默认地址请求信息
     * @return 结果
     */
    @Override
    @Transactional
    public int setDefaultAddress(ClientAddressSetDefaultRequestVo requestVo) {
        // 重置用户所有默认地址
        addressMapper.resetDefaultAddress(requestVo.getUserId());
        
        // 设置新的默认地址
        return addressMapper.setDefaultAddress(requestVo.getId(), requestVo.getUserId());
    }
} 